const http = require("../../../utils/http.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dataInfo: {},
        catalogId: "",
        show: false,
        catalogList: [],
        pageInfo: {
            page: 1,
            limit: 18,
            pages: 0
        },
        activeKey: 0
    },
    getCatalog() {
        http.get(`wx/vip/catalog/${this.data.catalogId}`).then(data => {
            if (data) {
                this.setData({
                    dataInfo: {...data}
                })
                this.setScroll();
                http.post('wx/vip/record/save', {
                    catalogId: this.data.catalogId,
                    bookId: this.data.dataInfo.catalog.book.bookId
                }).then(() => {

                })
            }
        })
    },
    changeCatalog(e) {
        const catalogId = e.currentTarget.dataset.id
        this.setData({
            catalogId,
            show: false
        })
        this.getCatalog()
    },
    setScroll() {
        wx.createSelectorQuery().select('#catalog').node().exec((res) => {
            const scrollView = res[0].node;
            scrollView.scrollTo({
                top: 0,
                animated: true,
                duration: 500
            })
        })
    },
    showCatalog() {
        this.setData({
            show: true,
            catalogList: []
        }, () => {
            this.getCatalogList()
        })
    },
    getCatalogList() {
        this.data.pageInfo.bookId = this.data.dataInfo.catalog.book.bookId
        http.get('wx/v1/book/catalog/page', this.data.pageInfo).then(data => {
            const catalogList = [...this.data.catalogList, ...data.records]
            const catalogId = this.data.catalogId
            let activeKey = 0;
            for (let i = 0; i < catalogList.length; i++) {
                if (catalogList[i].id == catalogId) {
                    activeKey = i;
                    break
                }
            }
            this.setData({
                catalogList,
                "pageInfo.pages": data.pages,
                activeKey
            })
        })
    },
    scrollToBottom() {
        if (this.data.pageInfo.page < this.data.pageInfo.pages) {
            this.setData({
                "pageInfo.page": this.data.pageInfo.page + 1
            })
            this.getCatalogList()
        } else {
            wx.showToast({title: "没有更多目录！"})
        }
    },
    closePop() {
        this.setData({
            show: false
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            catalogId: options.catalogId
        }, () => {
            this.getCatalog()
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
